﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS.UserControl
{
    public partial class ucDomainList : System.Web.UI.UserControl
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private ExpertBroker _expertBroker = new ExpertBroker();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshDomains();
        }

        private void RefreshDomains()
        {
            dlDomains.DataSource = _domainBroker.GetAllDomains();
            dlDomains.DataBind();

            dlDomains.Style["display"] = "none";
        }

        protected string GetDomainDescription(object domainOID)
        {
            if(domainOID == null || String.IsNullOrEmpty(domainOID.ToString())) return "";
            return string.Format("共有{0}位专家", _expertBroker.GetExpertCountByDomainOID(domainOID.ToString()));
        }

        public override string ClientID
        {
            get
            {
                return dlDomains.ClientID;
            }
        }

        protected void dlDomains_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DomainEntity domain = e.Item.DataItem as DomainEntity;
                Image imgDomainIcon = e.Item.FindControl("imgDomainIcon") as Image;
                if (domain != null && imgDomainIcon != null)
                {
                    string imgUrl = ExpertMIS.Codes.Utility.GetEnvironmentValue(domain.OID + ".icon");
                    if (!string.IsNullOrEmpty(imgUrl))
                    {
                        imgDomainIcon.ImageUrl = imgUrl;
                    }
                }
            }
        }
    }
}